package wre;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

public class WordTable {

	private static ArrayList<WordForm> wordTable = new ArrayList<WordForm>();

	/**
	 * This takes in two strings a string for the word and a string for the
	 * retort and generates a key for the retort using the hashCode method of
	 * the string class and checks to see if the
	 * 
	 * @param word
	 * @param retort
	 */

	public static ArrayList<WordForm> getInstance() {
		if (wordTable == null) {
			return wordTable = new ArrayList<WordForm>();
		}
		return wordTable;
	}

	public void addWord(String word, String retort) {
		ArrayList<WordForm> wordTable = WordTable.getInstance();
		// Adds a word to the table and generates the wordForm
		if (wordTable.contains(word)) {
			// Add another instance to the frequency table
			Iterator<WordForm> wordIter = wordTable.iterator();
			while (wordIter.hasNext()) {
				WordForm curr = wordIter.next();
				if (curr.getWord().equals(word)) {
					curr.insert(retort);
				}
			}
		} else {
			wordTable.add(new WordForm(word, retort.hashCode()));
		}
	}

	public Integer getRetort(String word) {
		ArrayList<WordForm> wordTable = WordTable.getInstance();
		Integer toReturn = null;
		// Searches the array for the word on the list
		Iterator<WordForm> wordIter = wordTable.iterator();
		while (wordIter.hasNext()) {
			WordForm curr = wordIter.next();
			if (curr.getWord().equals(word)) {
				toReturn = curr.getHighestFrequency();
			}
		}
		return toReturn;
	}

	public void saveWordTable() {
		String toFile;
		File saveFile = new File("wordTable.csv");

		DataOutputStream dataFOut = null;
		try {
			dataFOut = new DataOutputStream(new BufferedOutputStream(
					new FileOutputStream(saveFile)));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Saves the array to a database
		ArrayList<WordForm> currWordTable = WordTable.getInstance();
		Iterator<WordForm> wordIter = currWordTable.iterator();
		while (wordIter.hasNext()) {
			WordForm currWord = wordIter.next();
			toFile = "<w>\n" + currWord.getWord() + "\n";
			Map<Integer, Integer> currFreqTable = currWord.getMap();
			Iterator<Integer> keyIter = currFreqTable.keySet().iterator();
			toFile += "<f>\n";
			while (keyIter.hasNext()) {
				Integer currKey = keyIter.next();
				toFile += currKey + ", " + currFreqTable.get(currKey) + "\n";

			}
			try {
				dataFOut.writeChars(toFile);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void loadWordTable() {
		// Populates the array from the database
		File loadFile = new File("wordTable.csv");

		DataInputStream dataFIn = null;
		try {
			dataFIn = new DataInputStream(new BufferedInputStream(
					new FileInputStream(loadFile)));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			while (dataFIn.available() != 0) {
				String currString = dataFIn.readLine();
				if (currString.equals("<w>")) {
					String word = dataFIn.readLine();
					String nextLine = dataFIn.readLine();

				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
